package com.open.capacity.finance.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.exception.BusinessException;
import com.open.capacity.finance.config.Globals;
import com.open.capacity.finance.dao.PzDao;
import com.open.capacity.finance.dao.PzflDao;
import com.open.capacity.finance.dao.PzflExtDao;
import com.open.capacity.finance.dto.SlmxzzbDto;
import com.open.capacity.finance.entity.Pz;
import com.open.capacity.finance.entity.Pzfl;
import com.open.capacity.finance.entity.PzflExt;
import com.open.capacity.finance.service.PzService;
import com.open.capacity.finance.service.ReportService;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.reptile.entity.*;
import com.open.capacity.user.model.SysConfig;
import com.open.capacity.user.service.SysConfigService;
import com.open.capacity.user.service.SysDictDataService;
import com.open.capacity.user.service.SysDictService;
import com.open.capacity.utils.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


@Service
public class PzServiceImpl implements PzService {

    @Autowired
    private PzDao pzDao;

    @Autowired
    private PzflDao flDao;

    @Autowired
    private PzflExtDao extDao;

    @Autowired
    private ReportService reportService;

    @Autowired
    private SysConfigService sysConfig;

    @Autowired
    private SysDictDataService dictService;

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public Pz findById(String id) {
        Pz pz = pzDao.findById(id);
        if (pz == null) {
            Pzfl pzfl = flDao.findById(id);
            if (pzfl != null) {
                pz = pzDao.findById(pzfl.getPzid());
            }
        }
        String companyId = pz.getCompanyId();
        Integer kjnd = pz.getKjnd();
        if (pz != null) {
            //获取分录
            List<Pzfl> pzflList = flDao.findByPzId(pz.getPzid());
            List<Pzfl> pzflListMerge = new ArrayList<>();
            Pzfl item;

            TreeMap<Integer, List<Pzfl>> tempMap = pzflList.stream().collect(Collectors.groupingBy(c -> c.getFlxh() , TreeMap::new, Collectors.toList()));
            for (Map.Entry entry_item : tempMap.entrySet()) {
                item = new Pzfl();
                for (Pzfl fl : (List<Pzfl>) entry_item.getValue()) {
                    item.setZy(fl.getZy());
                    item.setFlid(fl.getFlid());
                    item.setFlxh(fl.getFlxh());
                    if ("1".equals(fl.getAccaCode())) {
                        item.setFlid1(fl.getFlid());
                        item.setKjkmCode1(fl.getKjkmCode());
                        item.setKjkmName1(fl.getKjkmName());
                        item.setKjkm1(fl.getKjkm());
                        item.setJ1(fl.getJ());
                        item.setD1(fl.getD());
                    } else if ("2".equals(fl.getAccaCode())) {
                        item.setFlid2(fl.getFlid());
                        item.setKjkmCode2(fl.getKjkmCode());
                        item.setKjkmName2(fl.getKjkmName());
                        item.setKjkm2(fl.getKjkm());
                        item.setJ2(fl.getJ());
                        item.setD2(fl.getD());
                    }
                    item.setJ(fl.getJ());
                    item.setD(fl.getD());
                }
                pzflListMerge.add(item);
            }

            Pz finalPz = pz;
            pzflListMerge.forEach(t -> {
                        Optional<GdszczMaEleAcco> kjkm1 = Globals.kjkmList.stream().filter(c -> c.getAgencyCode().equals(finalPz.getCompanyId()) && c.getSetYear().equals(finalPz.getKjnd()) && c.getChrCode().equals(t.getKjkmCode1())).findFirst();
                        if (kjkm1.isPresent()) {
                            t.setKjkmSimpleName1(kjkm1.get().getChrCode() + " " + kjkm1.get().getChrName());
                            t.setKjkmName1(kjkm1.get().getChrCode() + " " + kjkm1.get().getChrName());
                        }

                        Optional<GdszczMaEleAcco> kjkm2 = Globals.kjkmList.stream().filter(c -> c.getAgencyCode().equals(finalPz.getCompanyId()) && c.getSetYear().equals(finalPz.getKjnd()) && c.getChrCode().equals(t.getKjkmCode2())).findFirst();
                        if (kjkm2.isPresent()) {
                            t.setKjkmSimpleName2(kjkm2.get().getChrCode() + " " + kjkm2.get().getChrName());
                            t.setKjkmName2(kjkm2.get().getChrCode() + " " + kjkm2.get().getChrName());
                        }

                        List<PzflExt> extList1 = extDao.findByPzflId(t.getFlid2());
                        if (CollectionUtils.isNotEmpty(extList1)) {
                            Optional<PzflExt> expecoOption = extList1.stream().filter(ext -> StringUtils.isNotEmpty(ext.getExpecoCode()) && !ext.getExpecoCode().equals("*")).findFirst();
                            if (expecoOption.isPresent()) {
                                t.setIsExpecoHs(1);

                                extList1.forEach(ext -> {
                                    Optional<GdszczMaEleExpeco> expeco = Globals.expecoList.stream().filter(c -> c.getAgencyCode().equals(companyId) && c.getSetYear().equals(kjnd) && c.getChrCode().equals(ext.getExpecoCode())).findFirst();
                                    if (expeco.isPresent()) {
                                        ext.setExpecoName(expeco.get().getChrCode() + " " + expeco.get().getChrName());
                                    }
                                });
                            }

                            Optional<PzflExt> expfuncOption = extList1.stream().filter(ext -> StringUtils.isNotEmpty(ext.getExpfuncCode()) && !ext.getExpfuncCode().equals("*")).findFirst();
                            if (expfuncOption.isPresent()) {
                                t.setIsExpfuncHs(1);

                                extList1.forEach(ext -> {
                                    Optional<GdszczMaEleExpFunc> expfunc = Globals.expfuncList.stream().filter(c -> c.getAgencyCode().equals(companyId) && c.getSetYear().equals(kjnd) && c.getChrCode().equals(ext.getExpfuncCode())).findFirst();
                                    if (expfunc.isPresent()) {
                                        ext.setExpfuncName(expfunc.get().getChrCode() + " " + expfunc.get().getChrName());
                                    }
                                });
                            }

                            Optional<PzflExt> departmentOption = extList1.stream().filter(ext -> StringUtils.isNotEmpty(ext.getDepartmentCode()) && !ext.getDepartmentCode().equals("*")).findFirst();
                            if (departmentOption.isPresent()) {
                                t.setIsDepartmentHs(1);

                            }

                            Optional<PzflExt> depproOption = extList1.stream().filter(ext -> StringUtils.isNotEmpty(ext.getDepproCode()) && !ext.getDepproCode().equals("*")).findFirst();
                            if (depproOption.isPresent()) {
                                t.setIsDepproHs(1);

                                extList1.forEach(ext -> {
                                    Optional<GdszczMaEleDeppro> deppro = Globals.depproList.stream().filter(c -> c.getAgencyCode().equals(companyId) && c.getSetYear().equals(kjnd) && c.getChrCode().equals(ext.getDepproCode())).findFirst();
                                    if (deppro.isPresent()) {
                                        ext.setDepproName(deppro.get().getChrCode() + " " + deppro.get().getChrName());
                                    }
                                });
                            }

                            Optional<PzflExt> projectOption = extList1.stream().filter(ext -> StringUtils.isNotEmpty(ext.getProjectCode()) && !ext.getProjectCode().equals("*")).findFirst();
                            if (projectOption.isPresent()) {
                                t.setIsProjectHs(1);

                                extList1.forEach(ext -> {
                                    Optional<GdszczMaEleProject> project = Globals.projectList.stream().filter(c -> c.getAgencyCode().equals(companyId) && c.getSetYear().equals(kjnd) && c.getChrCode().equals(ext.getProjectCode())).findFirst();
                                    if (project.isPresent()) {
                                        ext.setProjectName(project.get().getChrCode() + " " + project.get().getChrName());
                                    }
                                });
                            }


                        }
                        t.setExtList(extList1);
                    }
            );
            pz.setPzflList(pzflListMerge);
        }
        return pz;
    }

    @Override
    public List<Pz> findExports(Map<String, Object> params) {
        String ztId = params.get("ztId").toString();
        List<Pz> pzList = findAll(params);

        if (CollectionUtils.isNotEmpty(pzList)) {

            pzList.forEach(pz -> {
                //获取分录
                List<Pzfl> pzflList = flDao.findByPzId(pz.getPzid());
                List<Pzfl> pzflList_print = new ArrayList<>(pzflList.size());
                BigDecimal total = BigDecimal.ZERO;
                for (Pzfl fl : pzflList) {
//                    fl.setJ(fl.getJ().setScale(2, BigDecimal.ROUND_DOWN));
//                    fl.setD(fl.getD().setScale(2, BigDecimal.ROUND_DOWN));
                    total = total.add(fl.getJ());

                    List<PzflExt> pzflExtList = extDao.findByPzflId(fl.getFlid());
                    Pzfl fl_print = new Pzfl();
                    if (CollectionUtils.isNotEmpty(pzflExtList)) {
                        for (PzflExt e : pzflExtList) {
                            String kjkm = fl.getKjkmName();
                            fl_print = new Pzfl();
                            BeanUtils.copyProperties(fl, fl_print);
                            if (StringUtil.isNotEmpty(e.getExpecoCode())) {
                                kjkm += "-" + e.getExpecoCode() + "";
                            }
                            if (StringUtil.isNotEmpty(e.getExpfuncCode())) {
                                kjkm += "-" + e.getExpfuncCode() + "";
                            }
                            if (StringUtil.isNotEmpty(e.getDepproCode())) {
                                kjkm += "-" + e.getDepproCode() + "";
                            }
                            if (StringUtil.isNotEmpty(e.getProjectCode())) {
                                kjkm += "-" + e.getProjectCode() + "";
                            }
                            if (StringUtil.isNotEmpty(e.getDepartmentCode())) {
                                kjkm += "-" + e.getDepartmentCode() + "";
                            }
                            fl_print.setJ((e.getJ() == null || e.getJ().compareTo(BigDecimal.ZERO) == 0) ? null : e.getJ());
                            fl_print.setD((e.getD() == null || e.getD().compareTo(BigDecimal.ZERO) == 0) ? null : e.getD());
                            fl_print.setKjkm(kjkm);
                            pzflList_print.add(fl_print);
                        }
                    } else {
                        String kjkm = fl.getKjkmName();
                        BeanUtils.copyProperties(fl, fl_print);
                        fl_print.setKjkm(kjkm);
                        fl_print.setJ(fl_print.getJ().compareTo(BigDecimal.ZERO) == 0 ? null : fl_print.getJ());
                        fl_print.setD(fl_print.getD().compareTo(BigDecimal.ZERO) == 0 ? null : fl_print.getD());
                        pzflList_print.add(fl_print);
                    }
                    //获取分录扩展
                    //fl.setExtList(extDao.findByPzflId(fl.getFlid()));
                }
                pz.setTotal2(total);
                pz.setTotal1(NumberToMoneyUtil.number2CNMontrayUnit(total));
                pz.setPzflList(pzflList_print);
                pz.setZtName(dictService.findNameByValue("ZT", ztId));
            });
        }
        return pzList;
    }

    /**
     * 查询记录
     *
     * @param ids
     * @return
     */
    public List<Pz> findByIds(String ids) {
        if (StringUtil.isEmpty(ids)) {
            return new ArrayList<>(0);
        }
        List<Pz> pzList = pzDao.findByIds(Arrays.asList(ids.split(",")));

        if (CollectionUtils.isNotEmpty(pzList)) {

            pzList.forEach(pz -> {
                //获取分录
                List<Pzfl> pzflList = flDao.findByPzId(pz.getPzid());
                List<Pzfl> pzflList_print = new ArrayList<>(pzflList.size());
                BigDecimal total = BigDecimal.ZERO;
                for (Pzfl fl : pzflList) {
                    total = total.add(fl.getJ());

                    Optional<GdszczMaEleAcco> kjkmOption = Globals.kjkmList.stream().filter(c -> c.getAgencyCode().equals(pz.getCompanyId()) && c.getSetYear().equals(pz.getKjnd()) && c.getChrCode().equals(fl.getKjkmCode())).findFirst();
                    if (kjkmOption.isPresent()) {
                        fl.setKjkmName(kjkmOption.get().getChrName());
                        fl.setKjkmSimpleName(kjkmOption.get().getChrCode() + " " + kjkmOption.get().getChrName());
                    }

                    List<PzflExt> pzflExtList = extDao.findByPzflId(fl.getFlid());
                    Pzfl fl_print = new Pzfl();
                    if (CollectionUtils.isNotEmpty(pzflExtList)) {
                        for (PzflExt e : pzflExtList) {
                            String kjkm = fl.getKjkmName();
                            fl_print = new Pzfl();
                            BeanUtils.copyProperties(fl, fl_print);
                            if (StringUtil.isNotEmpty(e.getExpecoCode())) {
                                if (StringUtil.isNotEmpty(e.getExpecoName())) {
                                    kjkm += "-" + e.getExpecoName().replaceAll(e.getExpecoCode(), "").replaceAll(" ", "") + "";
                                }
                            }
                            if (StringUtil.isNotEmpty(e.getExpfuncCode())) {
                                if (StringUtil.isNotEmpty(e.getExpfuncName())) {
                                    kjkm += "-" + e.getExpfuncName().replaceAll(e.getExpfuncCode(), "").replaceAll(" ", "") + "";
                                }
                            }
                            if (StringUtil.isNotEmpty(e.getDepproName())) {
                                kjkm += "-" + e.getDepproName() + "";
                            }
                            if (StringUtil.isNotEmpty(e.getProjectName())) {
                                kjkm += "-" + e.getProjectName() + "";
                            }
                            if (StringUtil.isNotEmpty(e.getDepartmentName())) {
                                kjkm += "-" + e.getDepartmentName() + "";
                            }
                            fl_print.setJ((e.getJ() == null || e.getJ().compareTo(BigDecimal.ZERO) == 0) ? null : e.getJ());
                            fl_print.setD((e.getD() == null || e.getD().compareTo(BigDecimal.ZERO) == 0) ? null : e.getD());
                            fl_print.setKjkm(kjkm.replaceAll("--", "-"));
                            pzflList_print.add(fl_print);
                        }
                    } else {
                        String kjkm = fl.getKjkmName();
                        BeanUtils.copyProperties(fl, fl_print);
                        fl_print.setKjkm(kjkm);
                        fl_print.setJ(fl_print.getJ().compareTo(BigDecimal.ZERO) == 0 ? null : fl_print.getJ());
                        fl_print.setD(fl_print.getD().compareTo(BigDecimal.ZERO) == 0 ? null : fl_print.getD());
                        pzflList_print.add(fl_print);
                    }
                    //获取分录扩展
                    //fl.setExtList(extDao.findByPzflId(fl.getFlid()));
                }
                pz.setTotal2(total);
                pz.setTotal1(NumberToMoneyUtil.number2CNMontrayUnit(total));
                pz.setPzflList(pzflList_print);
            });
        }

        return pzList;
    }

    @Override
    public Pz findPre(String id, Date pzrq, String pzlxCode) {
        Pz orgPz = pzDao.findById(id);
        if (orgPz == null) {
            return null;
        }
        Pz pz = pzDao.findPre(orgPz.getCompanyId(), orgPz.getZtId(), orgPz.getKjnd(), pzrq.getMonth() + 1, pzlxCode, orgPz.getPzh());
        return pz;
    }

    @Override
    public Pz findNext(String id, Date pzrq, String pzlxCode) {
        Pz orgPz = pzDao.findById(id);
        if (orgPz == null) {
            return null;
        }
        Pz pz = pzDao.findNext(orgPz.getCompanyId(), orgPz.getZtId(), orgPz.getKjnd(), pzrq.getMonth() + 1, pzlxCode, orgPz.getPzh());
        return pz;
    }

    //加工过滤参数
    private Map<String, Object> filterParam(Map<String, Object> params) {
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());

        if (params.containsKey("kjkmCodes")) {
            String kjkmCodes = params.get("kjkmCodes").toString();
            String[] kjkmArr = kjkmCodes.split(",");
            if (StringUtil.isEmpty(kjkmCodes.trim())) {
                params.remove("kjkmCodes");
            } else {
//                String[] kjkmExtArr = kjkmService.findChildCodes(companyId, ztId, kjnd, kjkmArr);
//                if (kjkmExtArr.length > 0) {
//                    params.put("kjkmCodes", kjkmExtArr);
//                    params.put("kjkmCodes_select", kjkmArr);
//                }
            }
        }
        if (params.containsKey("xmCodes")) {
            String xmCodes = params.get("xmCodes").toString();
            if (StringUtil.isEmpty(xmCodes.trim())) {
                params.remove("xmCodes");
            } else {
                String[] xmArr = xmCodes.split(",");
                params.put("xmCodes", xmArr);
            }
        }
        if (params.containsKey("bmCodes")) {
            String bmCodes = params.get("bmCodes").toString();
            if (StringUtil.isEmpty(bmCodes.trim())) {
                params.remove("bmCodes");
            } else {
                String[] bmArr = bmCodes.split(",");
                params.put("bmCodes", bmArr);
            }
        }
        boolean ajSelect = false;
        if (params.containsKey("ajCodes")) {
            String ajCodes = params.get("ajCodes").toString();
            if (StringUtil.isEmpty(ajCodes.trim())) {
                params.remove("ajCodes");
            } else {
                String[] ajArr = ajCodes.split(",");
                params.put("ajCodes", ajArr);
                ajSelect = true;
            }
        }
        boolean pjhSelect = false;
        if (params.containsKey("pjhCodes")) {
            String pjhCodes = params.get("pjhCodes").toString();
            if (StringUtil.isEmpty(pjhCodes.trim())) {
                params.remove("pjhCodes");
            } else {
                String[] pjhArr = pjhCodes.split(",");
                params.put("pjhCodes", pjhArr);
                pjhSelect = true;
            }
        }
        boolean zphSelect = false;
        if (params.containsKey("zphCodes")) {
            String zphCodes = params.get("zphCodes").toString();
            if (StringUtil.isEmpty(zphCodes.trim())) {
                params.remove("zphCodes");
            } else {
                String[] zphArr = zphCodes.split(",");
                params.put("zphCodes", zphArr);
                zphSelect = true;
            }
        }
        boolean wldxSelect = false;
        if (params.containsKey("wldxCodes")) {
            String wldxCodes = params.get("wldxCodes").toString();
            if (StringUtil.isEmpty(wldxCodes.trim())) {
                params.remove("wldxCodes");
            } else {
                String[] wldxArr = wldxCodes.split(",");
                params.put("wldxCodes", wldxArr);
                wldxSelect = true;
            }
        }
        if (!params.containsKey("ajCodes") && !ajSelect && params.containsKey("ajCodes_input")) {
            String ajCodes_input = params.get("ajCodes_input").toString();
            if (StringUtil.isNotEmpty(ajCodes_input.trim())) {
                params.remove("ajCodes");
                params.put("ajCode", ajCodes_input);
            }
        }
        if (!params.containsKey("pjhCodes") && !pjhSelect && params.containsKey("pjhCodes_input")) {
            String pjhCodes_input = params.get("pjhCodes_input").toString();
            if (StringUtil.isNotEmpty(pjhCodes_input.trim())) {
                params.remove("pjhCodes");
                params.put("pjhCode", pjhCodes_input);
            }
        }
        if (!params.containsKey("zphCodes") && !zphSelect && params.containsKey("zphCodes_input")) {
            String zphCodes_input = params.get("zphCodes_input").toString();
            if (StringUtil.isNotEmpty(zphCodes_input.trim())) {
                params.remove("zphCodes");
                params.put("zphCode", zphCodes_input);
            }
        }
        if (!params.containsKey("wldxCodes") && !wldxSelect && params.containsKey("wldxCodes_input")) {
            String wldxCodes_input = params.get("wldxCodes_input").toString();
            if (StringUtil.isNotEmpty(wldxCodes_input.trim())) {
                params.remove("wldxCodes");
                params.put("wldxCode", wldxCodes_input);
            }
        }
        return params;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<Pz> pageAll(Map<String, Object> params) {
        params = filterParam(params);
        boolean aj = params.containsKey("ajCodes") || params.containsKey("ajCode");
        boolean wldx = params.containsKey("wldxCodes") || params.containsKey("wldxCode");
        boolean pjh = params.containsKey("pjhCodes") || params.containsKey("pjhCode");
        boolean zph = params.containsKey("zphCodes") || params.containsKey("zphCode");
        boolean xm = params.containsKey("xmCodes");
        boolean bm = params.containsKey("bmCodes");

        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        boolean extj1 = params.containsKey("extjStart") && StringUtil.isNotEmpty(params.get("extjStart").toString());
        boolean extj2 = params.containsKey("extjEnd") && StringUtil.isNotEmpty(params.get("extjEnd").toString());
        boolean extd1 = params.containsKey("extdStart") && StringUtil.isNotEmpty(params.get("extdStart").toString());
        boolean extd2 = params.containsKey("extdEnd") && StringUtil.isNotEmpty(params.get("extdEnd").toString());
        boolean flid = params.containsKey("flid") && StringUtil.isNotEmpty(params.get("flid").toString());

        String sidx = params.get("sidx").toString();
        String sord = params.get("sord").toString();
        if (StringUtil.isNotEmpty(sidx) && StringUtil.isNotEmpty(sord)) {
            params.put("orderby", sidx + " " + sord);
        }

        List<Pz> list = new ArrayList<>();
        if (aj || wldx || xm || bm || pjh || zph || extd1 || extd2 || extj1 || extj2 || flid) {
            list = pzDao.findAll2(params);
        } else {
            list = pzDao.findAll1(params);
        }

        list.forEach(p -> {
            if (p.getIsTemp().equals(1)) {
                p.setCategory(1);
            } else if (p.getIsQc().equals(1)) {
                p.setCategory(2);
            } else if (p.getIsJz().equals(1)) {
                p.setCategory(3);
            }
            Pzfl simple = flDao.getSimple(p.getPzid());
            p.setZy(simple.getZy());
            p.setJ(simple.getJ());
        });
        PageInfo<Pz> pageInfo = new PageInfo(list);

        return PageResult.<Pz>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<Pz> findAll(Map<String, Object> params) {
        params = filterParam(params);
        boolean aj = params.containsKey("ajCodes") || params.containsKey("ajCode");
        boolean wldx = params.containsKey("wldxCodes") || params.containsKey("wldxCode");
        boolean pjh = params.containsKey("pjhCodes") || params.containsKey("pjhCode");
        boolean zph = params.containsKey("zphCodes") || params.containsKey("zphCode");
        boolean xm = params.containsKey("xmCodes");
        boolean bm = params.containsKey("bmCodes");


        List<Pz> pzList = new ArrayList<>();
        if (aj || wldx || xm || bm || pjh || zph) {
            pzList = pzDao.findAll2(params);
        } else {
            pzList = pzDao.findAll1(params);
        }

        pzList.forEach(item -> {
            item.setPzlx(item.getPzlxCode() + ' ' + item.getPzlxName());
        });
        return pzList;
    }

    /**
     * 列表(只列出凭证号)
     *
     * @param params
     * @return
     */
    public List<Pz> findAllSimple(Map<String, Object> params) {
        //凭证排序规则
        SysConfig config = sysConfig.findByKey("pztree_sort");
        if (config == null) {
            config = new SysConfig();
            config.setId("13");
            config.setCnfKey("pztree_sort");
            config.setCnfName("凭证列表排序");
            config.setCnfValue("1");
            config.setCnfValue("1:正序； 2:倒序");
            sysConfig.saveOrUpdate(config, "13");
        }
        if (config.getCnfValue() != null && config.getCnfValue().equals("1")) {   //正序
            params.put("orderby", "pzh asc");
        } else {
            params.put("orderby", "pzh desc");
        }
        List<Pz> pzList = pzDao.findAll_simple(params);
        pzList.forEach(item -> {
            item.setPzlx(item.getPzlxCode() + ' ' + item.getPzlxName());
        });
        return pzList;
    }

    /**
     * 列表(只列出凭证号)
     *
     * @param params
     * @return
     */
    public List<Pz> findAllSimple_hedge(Map<String, Object> params) {
        //凭证排序规则
        SysConfig config = sysConfig.findByKey("pztree_sort");
        if (config == null) {
            config = new SysConfig();
            config.setId("13");
            config.setCnfKey("pztree_sort");
            config.setCnfName("凭证列表排序");
            config.setCnfValue("1");
            config.setCnfValue("1:正序； 2:倒序");
            sysConfig.saveOrUpdate(config, "13");
        }
        if (config.getCnfValue() != null && config.getCnfValue().equals("1")) {   //正序
            params.put("orderby", "pzh asc");
        } else {
            params.put("orderby", "pzh desc");
        }
        List<Pz> pzList = pzDao.findAll_simple2(params);
        pzList.forEach(item -> {
            item.setPzlx(item.getPzlxCode() + ' ' + item.getPzlxName());
        });
        return pzList;
    }
}
